package com.scut.scutzone.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scut.scutzone.config.HttpSessionAttribute;
import com.scut.scutzone.config.ScutZoneConfig;
import com.scut.scutzone.service.DatabaseService;
import com.scut.scutzone.util.FileUtil;
import com.scut.scutzone.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/posts")
public class PostController {
    @Autowired
    private DatabaseService databaseService;
    @Autowired
    private ScutZoneConfig scutZoneConfig;

    @GetMapping("")
    @ResponseBody
    public Result getPosts(@RequestParam(name = "uuid", required = false) String uuid,
                           @RequestParam(name = "username", required = false) String username,
                           @RequestParam(name = "keyword", required = false) String keyword,
                           @RequestParam(name = "last_id", required = false, defaultValue = "0") Integer lastId,
                           @RequestParam(name = "limit", required = false, defaultValue = "5") Integer limit) {
        return Result.success("OK", databaseService.getPosts(uuid, username, keyword, lastId, limit));
    }

    @GetMapping("/{postId}")
    @ResponseBody
    public Result getPostById(@PathVariable Integer postId,
                              @RequestParam(name = "last_comment_id", required = false, defaultValue = "0") Integer lastCommentId,
                              @RequestParam(name = "limit", required = false, defaultValue = "5") Integer limit) {
        return Result.success("OK", databaseService.getPost(postId, lastCommentId, limit));
    }

    @PostMapping("")
    @ResponseBody
    public Result addPost(@RequestParam("content") String content,
                          @RequestParam(value = "imgs", required = false) MultipartFile[] imgArr,
                          HttpSession session) throws IOException {

        String uuid = (String) session.getAttribute(HttpSessionAttribute.USER_UUID);
        String[] imgName = FileUtil.saveImages(scutZoneConfig.getRoot_path()+scutZoneConfig.getPost_img_path(), imgArr);
        return Result.success("OK", databaseService.savePost(uuid, content, imgName));
    }

    @PutMapping("/{postId}")
    @ResponseBody
    public Result updatePost(@PathVariable Integer postId,
                             @RequestParam("content") String content,
                             @RequestParam(value = "imgs", required = false) MultipartFile[] imgArr,
                             HttpSession session) throws IOException {

        String uuid = (String) session.getAttribute(HttpSessionAttribute.USER_UUID);
        String[] imgName = FileUtil.saveImages(scutZoneConfig.getRoot_path()+scutZoneConfig.getPost_img_path(), imgArr);
        //databaseService.updatePost(postId,uuid,content,imgName);

        JSONObject object = new JSONObject();
        object.put("post_id", postId);
        return Result.success(object);
    }

    @DeleteMapping("/{post_id}")
    @ResponseBody
    public Result deletePost(@PathVariable Integer post_id,
                             HttpSession session) {
        databaseService.deletePost(post_id, (String) session.getAttribute(HttpSessionAttribute.USER_UUID));
        return Result.success(null);
    }
}
